package dex.studio.compose.news.ui.pages

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.sp
import dex.studio.compose.news.ui.NewsTopAppBar
import dex.studio.compose.news.ui.data.Repository
import dex.studio.compose.news.ui.data.bean.NewsBean
import dex.studio.compose.news.ui.theme.ComposeNewsColor
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Composable
fun NewsListPage(onNewsListClick: (newsBean: NewsBean) -> Unit) {
    //主线程
    val mainCoroutineScope = CoroutineScope(Dispatchers.IO)
    Scaffold(topBar = {
        NewsTopAppBar(title = "News List", isShowBack = false)
    }, content = {
        val tempEmptyList = mutableStateOf(mutableListOf<NewsBean>())
        var newsList by remember { tempEmptyList }
        var totalSize by remember {
            mutableStateOf(0)
        }

        rememberCoroutineScope().launch {
            val tempList = Repository.getNewsList()
            newsList = mutableListOf(*tempList.toTypedArray())
            totalSize = tempList.size

        }
        Column {
            Text(text = "total size == $totalSize")
            LazyColumn(
                modifier = Modifier
                    .fillMaxWidth()
                    .fillMaxHeight()
                    .background(Color.Red)
            ) {
                items(newsList)
                { newsBean ->
                    Text(
                        text = newsBean.title,
                        fontSize = 14.sp,
                        fontFamily = FontFamily.Monospace,
                        modifier = Modifier
                            .clickable(
                                onClick = {
                                    onNewsListClick(newsBean)
                                }
                            )
                            .background(Color.LightGray)
                            .fillMaxWidth()

                    )
                }

            }
        }


    })
}